package com.x.proxy.controller;

import com.x.proxy.entity.Product;
import com.x.proxy.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.time.LocalDateTime;
import java.util.Random;
import java.util.UUID;

/**
 * the class of springcloud
 *
 * <p>
 * .
 *
 * @author little_lunatic
 * @date 2024-05-22
 */
@RestController
@Slf4j
@RequestMapping("product")
public class ProductController {

    @Resource
    private ProductService productService;

//    @Resource
//    private IdService idService;

    @GetMapping("add")
    public Boolean add() throws UnsupportedEncodingException {
        Product product = new Product();
//        product.setId(idService.generateId("product"));
        long id = new Random().nextInt(10000);
        product.setId(id);
        product.setName(URLEncoder.encode("商品名称：" + UUID.randomUUID(), "UTF-8"));
        product.setCreateTime(LocalDateTime.now());
        product.setUpdateTime(LocalDateTime.now());
        boolean save = productService.save(product);
        Product productServiceById = productService.getById(id);
        productServiceById.setName(URLDecoder.decode(productServiceById.getName(), "UTF-8"));
        log.info("{}", productServiceById);
        return save;
    }
}
